Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

finding if a character is repeated more than once.

+1 vote
asked Oct 31, 2019 by MCg GDcheeriosYT (130 points)
import random
import itertools
import collections
res = [random.randrange(1, 8, 1) for i in range(1)]
n1 = [1]
n2 = [2]
n3 = [3]
n4 = [4]
n5 = [5]
n6 = [6]
n7 = [7]
f= open("memory.txt","a+")
f.write("%s\n" % (res))
f= open("memory.txt","r")
f1 = f.readlines()
class cereal:
    def __init__(self, name, flavor, type,):
        self.name = name
        self.flavor = flavor
        self.type = type

o = "round"
c = "crunchy"
w = "wafer"
s = "soft"
p = "part crunchy part soft"
cheerios_special = cereal("cheerios with a hint of syrup", "incredible", o)
cheerios = cereal("cheerios", "bland", o)
fruit_loops = cereal("fruit loops", "fruity", o)
captain_crunch = cereal("captain crunch", "captain crunchy", c)
life_norm = cereal("Life", "bland", w)
life_cin = cereal("Life", "cinamon", w)
lucky_charms = cereal("lucky charms", "magical", p)
x1 = cheerios.name
x2 = cheerios.flavor
x3 = cheerios.type
y1 = fruit_loops.name
y2 = fruit_loops.flavor
y3 = fruit_loops.type
z1 = captain_crunch.name
z2 = captain_crunch.flavor
z3 = captain_crunch.type
l1 = life_norm.name
l2 = life_norm.flavor
l3 = life_norm.type
l4 = life_cin.name
l5 = life_cin.flavor
l6 = life_cin.type
c1 = lucky_charms.name
c2 = lucky_charms.flavor
c3 = lucky_charms.type
o1 = cheerios_special.name
o2 = cheerios_special.flavor
o3 = cheerios_special.type
if res == [1]:
    print("you should %s %s it tastes %s and is %s" % (x1, sr1, x2, x3))
if res == [2]:
    print("you should %s %s it tastes %s and is %s" % (y1, sr2, y2, y3))
if res == [3]:
    print("you should %s %s it tastes %s and is %s" % (z1, sr3, z2, z3))
if res == [4]:
    print("you should %s %s it tastes %s and is %s" % (l1, sr4, l2, l3))
if res == [5]:
    print("you should %s %s it tastes %s and is %s" % (l4, sr5, l5, l6))
if res == [6]:
    print("you should %s %s it tastes %s and is %s" % (c1, sr6, c2, c3))
if res == [7]:
    print("you should %s %s it tastes %s and is %s" % (o1, sr7, o2, o3))

that is my current code the for is empty because I didn't finish I need to find a way to count characters that are repeated in a file and then change a variable if it is counted 2 or more.

2 Answers

0 votes
answered Nov 3, 2019 by gameforcer (2,990 points)

I don't know what characters are you trying to count but you might want to look into counter type of objects from collections module.

By the way please don't make so many weird variables. If you want to have a group of data defined then put it into a list, set, tuple or dictionary. For example

o = "round"
c = "crunchy"
w = "wafer"
s = "soft"

could be just

type_dict = {'o': 'round', 'c': 'crunchy', 'w': 'wafer', 's': 'soft'}

An example of use:


 Above will print out "round".

0 votes
answered Nov 3, 2019 by Rohan Ghobade (520 points)
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.