what is Fortran equivalent of the following statement [duplicate] - string

This question already has answers here:
Convert integers to strings to create output filenames at run time
(9 answers)
Closed 3 years ago.
I want to assign a formatted string to a variable. For example, I would write the following in Python:
my_score = 100
line = "score = %d" % my_score
print(line)
This will print the following:
score = 100
How to write the same in Fortran?

The direct implementation of Your code would be something like:
program test
integer :: score
character(len=30) :: line
score = 100
write(line, '(a, i3)') "score = ", score
print '(a)', line
end program test

Related

How to generate a random order of numbers to a string based on conditions Python [duplicate]

This question already has answers here:
Generate random numbers only with specific digits
(3 answers)
Closed 2 years ago.
How would you generate a random number string in Python based on the following conditions.
The string must be between the length of 3 and 7
The string must only contain numbers from 1-7
The string must not have spaces
I tried the following for the string output but I am struggling with the conditionals
letters = string.digits
print ( ''.join(random.choice(letters) for i in range(10)) )
The output I received was:=
9432814671
If you could be kind enough to help me out and guide me I would be grateful to you.
The solution is self-explanatory, and you were close to it:
length = random.randint(3, 7)
"".join(str(random.randint(1, 7)) for _ in range(length))
#'724613'

What will be the correct indentation of this python3 code [duplicate]

This question already has answers here:
Indentation Error in Python [duplicate]
(7 answers)
Closed 2 years ago.
I'm getting the error
for quote in quotes:
^
IndentationError: unindent does not match any o
uter indentation level
code below :-
if __name__ == "__main__":
# Query the price once every N seconds.
for _ in iter(range(N)):
quotes = json.loads(urllib.request.urlopen(QUERY.format(random.random())).read())
""" ----------- Update to get the ratio --------------- """
prices = {}
for quote in quotes:
stock, bid_price, ask_price, price = getDataPoint(quote)
print ("Quoted %s at (bid:%s, ask:%s, price:%s)" % (stock,bid_price, ask_price, price))
print ("Ratio %s" % getRatio(prices['ABC'], prices['DEF']))
Check brackets again in here whether they placed correctly.
quotes = json.loads(urllib.request.urlopen(QUERY.format(random.random())).read())

How can i get the sum string using python? [duplicate]

This question already has answers here:
Python error: "IndexError: string index out of range"
(4 answers)
Closed 3 years ago.
Here is my program I want to find the weather it is a sum string or not based on the following condition
1)the string length must be >3
example:"12358" --- 1+2=3,2+3=5,3+5=8
I tried this program I am getting the index error please help me.Thank you in adavnce.
Given below is my code:
y="12358"
for i in range(len(y)-1):
if y[i]+y[i+1]==y[i+2]:
print "sum stringgg"
The upper bound of the range should be the length of y minus 2 instead to accommodate the comparison with the item of the index plus 2. You should also convert each character in the string to an integer for arithmetic addition and comparison. Finally, you should use the for-else construct to break whenever the former two digits do not add up to the latter digit, and only output 'sum string' if the loop finishes without break:
y = "12358"
digits = list(map(int, y))
for i in range(len(digits) - 2):
if digits[i] + digits[i + 1] != digits[i + 2]:
break
else:
print('sum string')

how to count specific character in text file using python? [duplicate]

This question already has answers here:
Count the number of occurrences of a character in a string
(26 answers)
Closed 4 years ago.
#Exercise 7: Counting...1...2...3...
#The purpose of this program is to ask a file from the user, open the file
# and counts the number of comma-separated values in it and report the result to the user
name_file = input("Enter name of file: ")
inf = open(name_file, "r")
count_comma = 0
line = inf.readline()
for char in line:
if "," in char:
count_comma +=1
print (count_comma)
inf.close()
when i run it prints 0. why?
you're probably better off with
count_comma = len(line.split(","))
but you'd have to run timeit to be sure

Python, how to write a message that includes a tuple and a string of characters when using input? [duplicate]

This question already has answers here:
How do I put a variable’s value inside a string (interpolate it into the string)?
(9 answers)
Closed 4 years ago.
I am trying to write a message to the user that contains a tuple and string something like
(0,0) what is your move:
i tried the following:
x= input((0,0)+ "what is your move: ") cant concatenate str with tuple
x= input((0,0), "what is your move: ") input takes a max of 1 arg
x= input(((0,0), "whats your move))" result: (0,0) whats your move: )
You have to convert the tuple to a string first:
x = input(str((0,0)) + " what is your move: ")
Though it would be cleaner to use the str.format method:
x = input("{} what is your move: ".format((0,0)))

Resources